#!/usr/bin/python
# -*- coding: utf-8 -*-
import logging
import time

# NullHandler class for lib coding purpose
class NullHandler(logging.Handler):
    def emit(self, record):
        pass
    
# A handler based on a queue. Records are just put on a queue
class QueueHandler(logging.Handler):
    def __init__(self, queue):
        logging.Handler.__init__(self)
        self._queue = queue
        
    def emit(self, record):
        self._queue.put_nowait(record)

# Queue log manager
class QueuedLogManager(object):
    @staticmethod
    def log_from_queue_for_ever(logger, queue):
        while True:
            try:
                if not queue.empty():
                    record = queue.get_nowait()
                    logger.callHandlers(record)
                else:
                    time.sleep(1)
            except:
                time.sleep(1)

class TestLogger(object):
    def __init__(self, queue):
        self._queue = queue
        
    def info(self, message):
        record = logging.makeLogRecord({"name": "test_queue_logger.local", "msg": message, "levelno": logging.INFO, "levelname": logging.getLevelName(logging.INFO)})
        self._queue.put_nowait(record)